<?php
/**
 * 系统用户类
 */
namespace app\admin\controller;

use think\App;
use think\facade\Request;
use think\facade\Config;

class Uploader extends Common{
    private $typeArr = '';

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->typeArr = array("jpg", "png", "gif","ico");
    }
    
    public function ajaxUpload(Request $Request){
        $name = $_FILES['file']['name'];
        $size = $_FILES['file']['size'];
        $name_tmp = $_FILES['file']['tmp_name'];
        if (empty($name)) {
            return json(array("error" => "您还未选择图片"));
        }
        $type = strtolower(substr(strrchr($name, '.'), 1));
        //获取文件类型

        if (!in_array($type, $this->typeArr)) {
            return json(array("error" => "清上传jpg,png或gif类型的图片！"));
        }
        if ($size > (2 * 1024 * 1024)) {
            return json(array("error" => "图片大小已超过500KB！"));
        }

        $pic_name = date("YmdHis", time()) . rand(10000, 99999) . "." . $type;
        //图片名称
        $pic_url = app()->getRootPath() . 'public/uploads/images/' . $pic_name;

        //上传后图片路径+名称
        if (move_uploaded_file($name_tmp, $pic_url)) {//临时文件转移到目标文件夹
            //return json(array("error" => "0", "pic" => DS . 'thinkPHP'. DS . $pic_url, "name" => $pic_name));
            return json(array("error" => "0", "pic" => '/uploads/images/' . $pic_name, "name" => $pic_name));
        } else {
            return json(array("error" => "上传有误，清检查服务器配置！"));
        }
    } 
}
?>